/**
 * Aistarfish.com Inc.
 * Copyright (c) 2017-2019 All Rights Reserved.
 */
package com.xf.control;

import java.io.IOException;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.qingstor.sdk.exception.QSException;
import com.xf.model.App;
import com.xf.model.PageParam;
import com.xf.model.PageResult;
import com.xf.service.AppService;
import com.xf.service.FileService;

/**
 *
 * @author xiangfeng
 * Created by on 2019-12-17 16:24
 */
public class AppController extends Controller {

    @Inject
    private AppService appService;

    @Inject
    private FileService fileService;

    public void addPage(){
        render("add.html");
    }

    public void add() throws QSException, IOException {
        UploadFile logo = getFile("logo");
        String logoUrl = getPara("logoUrl");
        UploadFile ico = getFile("ico");
        String icoUrl = getPara("icoUrl");
        String name = getPara("name");
        String redirectUrl = getPara("redirectUrl");
        Boolean open = getBoolean("open");
        Integer type = getInt("type");

        appService.addApp(logo,logoUrl,ico,icoUrl,name,redirectUrl,open,type);

        setAttr("result",Boolean.TRUE);
        redirect("/index");
    }

    public void listPage() {
        render("list.html");
    }

    public void list(){
        PageParam pageParam = new PageParam();
        pageParam.setPageIndex(getInt("pageIndex"));
        pageParam.setPageSize(getInt("pageSize"));
        Page<App> page = appService.getAppByPage(pageParam);
        PageResult<App> result = new PageResult<>();
        result.setRows(page.getList());
        result.setTotal(page.getTotalRow());
        renderJson(result);
    }

    public void update() throws IOException, QSException {
        App app = new App();
        UploadFile logo = getFile("logo");
        String logoUrl = getPara("logoUrl");
        UploadFile ico = getFile("ico");
        String icoUrl = getPara("icoUrl");
        String name = getPara("name");
        String redirectUrl = getPara("redirectUrl");
        Boolean open = getBoolean("open");
        Integer id = getParaToInt("id");
        Integer type = getInt("type");
        app.setName(name);
        app.setRedirectUrl(redirectUrl);
        app.setOpen(open);
        app.setId(id);
        app.setType(type);
        appService.update(app,logo,logoUrl,ico,icoUrl);
        setAttr("success",true);
        renderJson();
    }

    public void del(){
        Integer id = getParaToInt("id");
        appService.delApp(id);
        renderJson();
    }

}
